package com.bw.kotlindemo.kotlin.view.activity

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import androidx.lifecycle.ViewModelProviders
import com.bw.kotlindemo.R
import com.bw.kotlindemo.databinding.ActivityLiveListBinding
import com.bw.kotlindemo.java.utils.ToastUtil
import com.bw.kotlindemo.kotlin.base.KBaseActivity
import com.bw.kotlindemo.kotlin.bean.KLiveTypeBean
import com.bw.kotlindemo.kotlin.kvm.KLiveVM
import com.bw.kotlindemo.kotlin.view.fragment.KLiveListFragment

/**
 * desc:直播列表界面
 * create by xushaodong on 2023/4/5
 */
class KLiveListActivity :KBaseActivity<KLiveVM,ActivityLiveListBinding>(){
    var list: MutableList<Fragment> = mutableListOf()
    lateinit var mTypeBean: KLiveTypeBean
    override fun initData() {
        vm.liveTypeData.observe(this){
            if (it.status== "0000") {
                mTypeBean = it
                initView() // 网络请求到数据后，再去初始化view，因为刚打开页面，还没有tab数据
            } else {
                ToastUtil.showToast(it.message)
            }
        }
        loadData()
    }

    private fun initView() {
        for (resultBean in mTypeBean.result) {
            list.add(KLiveListFragment(resultBean))
        }
        mDataBinding.viewPager.adapter = object : FragmentPagerAdapter(supportFragmentManager) {
            override fun getItem(position: Int): Fragment {
                return list[position]
            }

            override fun getCount(): Int {
                return list.size
            }

            override fun getPageTitle(position: Int): CharSequence? {
                return mTypeBean.result.get(position).name
            }
        }
        mDataBinding.tabLayout.setupWithViewPager(mDataBinding.viewPager)
    }

    private fun loadData() {
        vm.getTypeList()
    }


    override fun initLayout(): Int {
       return R.layout.activity_live_list
    }


    override fun initViewModel(): KLiveVM {
        return ViewModelProviders.of(this).get(KLiveVM::class.java)
    }

}